package nciae.db.controller;

import lombok.extern.slf4j.Slf4j;
import nciae.db.common.CommonResult;
import nciae.db.pojo.dto.ApiDeleteFileDTO;
import nciae.db.pojo.dto.ApiIsSubmitHomeWorkDTO;
import nciae.db.pojo.entity.User;
import nciae.db.pojo.vo.HomeworkDetailVo;
import nciae.db.service.HomeworkDetailService;
import nciae.db.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@CrossOrigin
public class HomeworkDetailController {
    @Autowired
    private HomeworkDetailService homeworkDetailService;
    @ResponseBody
    @RequestMapping("/homeworkdetail/list")
    public CommonResult<List<HomeworkDetailVo>> list(int page, int limit, String student , Integer select, String homeworkType, HttpSession session){
        User user = (User) session.getAttribute("user");
        String expertId=user.getUserName();
        return homeworkDetailService.search(page,limit,student,select,homeworkType, expertId);
    }
    @GetMapping("/homeworkdetail/form")
    public ModelAndView homeworkCorrect(Integer id){
        HomeworkDetailVo homeworkDetailVo;
        homeworkDetailVo= homeworkDetailService.getHomeworkDetail(id);
        if(homeworkDetailVo == null){
            return new ModelAndView("error/404");
        }
        ModelAndView view = new ModelAndView("homework/homeworkCorrect");
        view.addObject("model", homeworkDetailVo);
        return view;
    }

    @ResponseBody
    @GetMapping("/homeworkdetail/downloadFiles")
    public ResponseEntity<Resource> downloadFiles(Integer id){
        return homeworkDetailService.downloadFiles(id);
    }
    @PostMapping("/homeworkdetail/save")
    public CommonResult<String> save(@RequestBody HomeworkDetailVo homeworkDetailVo){
        return homeworkDetailService.homeworkDetailSave(homeworkDetailVo);
    }

    @PostMapping("/api/apiIsSubmitHomeWork")
    @ResponseBody
    public CommonResult<Boolean> apiIsSubmitHomeWork(@RequestBody ApiIsSubmitHomeWorkDTO apiIsSubmitHomeWorkDTO){
        return homeworkDetailService.apiIsSubmitHomeWork(apiIsSubmitHomeWorkDTO);
    }

    @PostMapping("/api/UploadHomeWorkFiles")
    @ResponseBody
    public CommonResult<String> UploadHomeWorkFiles(
            @RequestParam("file") MultipartFile file,
            @RequestParam("studentId") Long studentId,
            @RequestParam("homeWorkId") Long homeWorkId) throws IOException {
        //文件上传并将路径存入数据库
        return homeworkDetailService.UploadHomeWorkFiles(file,studentId, homeWorkId);
    }

    @PostMapping("/api/deleteFiles")
    @ResponseBody
    public CommonResult<String> deleteFiles(@RequestBody ApiDeleteFileDTO apiDeleteFileDTO) throws IOException {
        System.out.println("ssssssssssssssss");
        System.out.println(apiDeleteFileDTO);
        return FileUploadUtils.deleteHomeWorkFile(apiDeleteFileDTO);
    }

}
